package wassalt.Utils;

import static wassalt.Utils.AppConstants.URL;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.*;
import wassalt.Utils.AppConstants;
import wassalt.Services.HTTPService;
import wassalt.client.R;

public class User {

	/**
	 * User Object data
	 */
	long id;
	String firstName;
	String lastName;
	String mail;
	String phone;
	/**
	 * set Device Registration key web service path
	 */
	public static final String SETDEVICEREGKEY_PATH = "/ManageUsers/setDeviceRegKey";

	public User() {
		id = -1;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public User(long id, String firstName, String lastName, String mail,
			String phone) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.mail = mail;
		this.phone = phone;
	}

	/**
	 * This static method saves the registered person data in shared preferences
	 * file.
	 * 
	 * @param context
	 * @param userData
	 * @throws JSONException
	 * @author Gad
	 * 
	 */
	public static void saveUser(Context context, JSONObject userData)
			throws JSONException {

		SharedPreferences sharedPreferences = context.getSharedPreferences(
				AppConstants.SHARED_PREFS, Context.MODE_WORLD_WRITEABLE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putLong(AppConstants.USER_ID,
				userData.getLong(AppConstants.USER_ID));
		editor.putString(AppConstants.FIRST_NAME,
				userData.getString(AppConstants.FIRST_NAME));
		editor.putString(AppConstants.LAST_NAME,
				userData.getString(AppConstants.LAST_NAME));
		editor.putString(AppConstants.PHONE,
				userData.getString(AppConstants.PHONE));
		editor.putString(AppConstants.MAIL,
				userData.getString(AppConstants.MAIL));
		editor.commit();

	}

	/**
	 * This static method returns a the current user of the application from
	 * shared preferences.
	 * 
	 * @param context
	 * @return User the returned user object
	 * @author Gad
	 */
	public static User loadUser(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				AppConstants.SHARED_PREFS, Context.MODE_WORLD_READABLE);
		long idd = sharedPreferences.getLong(AppConstants.USER_ID, -1);
		String f = sharedPreferences.getString(AppConstants.FIRST_NAME, null);
		String l = sharedPreferences.getString(AppConstants.LAST_NAME, null);
		String mob = sharedPreferences.getString(AppConstants.PHONE, null);
		String em = sharedPreferences.getString(AppConstants.MAIL, null);
		return new User(idd, f, l, em, mob);
	}

	/**
	 * Set variable registered in the saved preferences
	 * 
	 * @param context
	 * @author gad
	 * 
	 */
	public static void setRegistered(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				AppConstants.SHARED_PREFS, Context.MODE_WORLD_READABLE);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putBoolean(AppConstants.IS_REGISTERED, true);
		editor.commit();
	}

	/**
	 * This static method checks if the mobile is registered by checking shared
	 * preferences file if is Registered is set to true
	 * 
	 * @param context
	 * @return boolean if the user is registered or not
	 * @author Gad
	 */
	public static boolean isRegistered(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				AppConstants.SHARED_PREFS, Context.MODE_WORLD_READABLE);
		return sharedPreferences.getBoolean(AppConstants.IS_REGISTERED, false);
	}

	/**
	 * This static method returns the User's phone number to external functions
	 * 
	 * @param context
	 * @return Phone number
	 * @author Aya Kamel
	 */
	public static String registeredPhone(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				AppConstants.SHARED_PREFS, Context.MODE_WORLD_READABLE);
		return sharedPreferences.getString(AppConstants.PHONE, null);
	}

	/**
	 * This static method return the User's id to external functions
	 * 
	 * @param context
	 *            Used context for calling this method to get shared prefrences
	 * @return Id
	 * @author Aya Kamel
	 */
	public static Long registeredId(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				AppConstants.SHARED_PREFS, Context.MODE_WORLD_READABLE);
		return sharedPreferences.getLong(AppConstants.USER_ID, -1);
	}

	/**
	 * This method is fired when a new C2DM key is received. It should update
	 * the saved key in the shared preferences and fires http intent service to
	 * register this key on the server
	 * 
	 * @param context
	 *            Used context for calling this method to get shared prefrences
	 * @param deviceRegKey
	 *            C2DM registration key
	 * 
	 * @author Gad
	 */
	public static void updateDeviceID(Context context, String deviceRegKey) {
		// get user phone number from shared preferences
		String phone = registeredPhone(context);
		// fires the service intent to set the key on the server
		Intent serviceIntent = new Intent(context, HTTPService.class);
		serviceIntent.setAction(AppConstants.C2DM_RESPONSE_FILTER);
		// Adding url and params to extras
		serviceIntent.putExtra(URL, context.getString(R.string.server_url)
				+ SETDEVICEREGKEY_PATH);
		serviceIntent.putExtra(AppConstants.PHONE, phone);
		serviceIntent.putExtra(AppConstants.DEVICE_REG_KEY, deviceRegKey);
		context.startService(serviceIntent);
	}

}
